<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once 'config.inc.php';
require_once 'common.inc.php';

session_name($conf['session_name']);
session_start();

session_unset();

$db =& DBUtil::connect($conf);
$reg =& Registry::instance();
$reg->setEntry('db', $db);
$reg->setEntry('tbl_pref', $conf['tbl_pref']);
$reg->setEntry('conf', $conf);


$auth = false;
$do_login = false;

if (isset($_POST['login_username']) && isset($_POST['login_password'])) {

	$priv = &new AuthPriv();
	$priv->ip = ($conf['auth_check_ip']) ? WebUtil::getIP() : '';
	$auth = $priv->doAuth($_POST['login_username'], $_POST['login_password']);
	$do_login = true;
}


if ($auth == false) {
	sleep(1); // for brute attack

	$tpl = new tplTemplatez(APP_TMPL_DIR . 'login.html');
	$tpl->tplAssign('meta_charset', $conf['lang']['meta_charset']);

	if($do_login) {
		$file = AppMsg::getCommonMsgFile('error_msg.ini');
		$msgs =& AppMsg::parseMsgs($file, 'login_failed');		

		$tpl->tplAssign('error_msg', BoxMsg::factory('error', $msgs));
	
	} elseif(@$_GET['msg'] == 'auth_expired') {
		$file = AppMsg::getCommonMsgFile('error_msg.ini');
		$msgs =& AppMsg::parseMsgs($file, 'auth_expired');		

		$tpl->tplAssign('error_msg', BoxMsg::factory('error', $msgs));
	}
	
	if(APP_DEMO_MODE) {
		$tpl->tplSetNeeded('/demo');
	}
	
	$tpl->tplAssign(AppMsg::getMsgs('common_msg.ini'));
	
	$tpl->tplParse($_POST);
	echo $tpl->tplPrint(1);

} else {
	
	$page = (isset($_GET['ref'])) ? sprintf('go.php?ref=%s', $_GET['ref']) : 'go.php';
	header('Location: ' . APP_HOME_PATH . $page);
	exit();
}
?>
